﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommandExecuter;
using Variables;
using CsharpGenerator;
using PHPGenerator;
namespace DMPL
{
    class Program
    {
        static void Main(string[] args)
        {

            string sourcepath = "source.dmpl";
            string exportpath = "export.txt";
            string bytecode = "dmpl";
            int i = 0;
            foreach (string arg in args)
            {
                if (arg == "-sc")
                { 
                    sourcepath = args[i+1];
                }
                else if (arg == "-ex")
                { 
                    exportpath = args[i+1];
                }
                else if (arg == "-bc")
                { 
                    bytecode = args[i+1];
                }
                i++;
            }
            if (bytecode == "php")
            {
                PHPGenerator.PHPGenerator php = new PHPGenerator.PHPGenerator();
                php.GenerateCode(sourcepath, exportpath);
            }
            else if (bytecode == "dmpl")
            {
                Compile(sourcepath);
            }
            /*string path = Console.ReadLine();
            string path2 = Console.ReadLine();
            //a.GenerateCode(path, path);
            php.GenerateCode(path, path2);
            //Compile(path);
            Console.Read();
             */

        }
        static void Compile(string path)
        {
            Variable vr = new Variable();
            Variables.Array ara = new Variables.Array();
            vr.AddStartVars();
            CommandExecuter.CommandExecuter ce = new CommandExecuter.CommandExecuter(ref vr, ref ara);
            if (!File.Exists(path))
            {
                Console.WriteLine("File specified does not exists!!!");
                Console.Read();
                Environment.Exit(1);
            }

            StreamReader sr = new StreamReader(path);
            string code = sr.ReadToEnd();
            string[] lines = code.Split(new string[] {"\r\n"}, StringSplitOptions.None);
            foreach (string line in lines)
            {
                ce.ExecuteCommand(line);
            }
            sr.Close();
        }
    }
}
